<?php
/*
 * @Author: your name
 * @Date: 2020-06-05 17:32:41
 * @LastEditTime: 2020-12-28 09:51:29
 * @LastEditors: Please set LastEditors
 * @Description: 
 * @FilePath: /special/application/common/service/HostAccessLog.php
 */

namespace app\common\service\module_manage\netfilter;


use app\common\model\access_log\HostAccessLogModel;
use think\Db;

class HostAccessLog

{
    public function list($hostId, $params, $field = '*', $action = 'detail')
    {
        //分库查询处理
        $logModel = new HostAccessLogModel();
        $logModel->setHostId($hostId);
        if($action == 'list'){
            $logModel = $logModel->field($field)->group('source_ip, target_ip');
        }else{
            $logModel = $logModel->field($field);
        }

        //搜索条件
        isset($params['source_ip']) && !empty($params['source_ip']) ?  $logModel->where('source_ip','like', '%'.$params['source_ip'].'%' ) : '';
        isset($params['direction']) && is_numeric($params['direction']) ?  $logModel->where('direction', $params['direction']) : '';
        isset($params['target_ip']) && !empty($params['target_ip']) ?  $logModel->where('target_ip','like', '%'.$params['target_ip'].'%' ) : '';
        isset($params['target_port']) && is_numeric($params['target_port']) ?  $logModel->where('target_port', $params['target_port']) : '';
        isset($params['protocols']) && is_numeric($params['protocols']) ?  $logModel->where('protocols', $params['protocols']) : '';
        isset($params['permission']) && is_numeric($params['permission']) ?  $logModel->where('permission', $params['permission']) : '';
        isset($params['target_port']) && is_numeric($params['target_port']) ? $logModel->where('target_port','like', '%'.$params['target_port'].'%' ) : '';

        if (isset($params['first_time']) && !empty($params['first_time'])){
            $firstTime = strtotime($params['first_time']);
            $logModel->where('first_time','between',[$firstTime, $firstTime + 86399]);
        }

        if (isset($params['last_time']) && !empty($params['last_time'])){
            $lastTime = strtotime($params['last_time']);
            $logModel->where('last_time','between',[$lastTime, $lastTime + 86399]);
        }

        isset($params['ignore']) && is_numeric($params['ignore']) ?  $logModel->where('ignore', $params['ignore']) : '';

        $list = $logModel->paginate()->toArray();
        return $list;
    }

    /**
     * @name 修改
     * @param $condition
     * @param $updateData
     * @return int|string
     * @author wx
     * @date 2019/11/28 9:18
     */
    public function modify($hostId,$condition, $updateData)
    {
        $logModel = new HostAccessLogModel();
        $logModel->setHostId($hostId);
        return $logModel->where($condition)->update($updateData);
    }


    /**
     * @name 获取总数
     * @param $condition
     * @return float|string
     * @author wx
     * @date 2019/11/28 9:20
     */
    public function gettotal($hostId,$condition)
    {
        $logModel = new HostAccessLogModel();
        $logModel->setHostId($hostId);
        return $logModel->where($condition)->count();
    }

    /**
     * @name 获取出栈总条数
     * @param $condition
     * @return float|string
     * @author wx
     * @date 2019/11/28 9:50
     */
    public function getTotalOut($hostId,$condition)
    {
        $logModel = new HostAccessLogModel();
        $logModel->setHostId($hostId);
        $condition['direction'] = 1;//出栈
        return $logModel->where($condition)->count();
    }

    /**
     * @name 入栈总条数         //direction 流量方向 1出站 2进站
     * @param $condition
     * @return float|string
     * @author wx
     * @date 2019/11/28 9:50
     */
    public function getTotalInput($hostId,$condition)
    {
        $logModel = new HostAccessLogModel();
        $logModel->setHostId($hostId);
        $condition['direction'] = 2;//入栈
        return $logModel->where($condition)->count();
    }
}